package com.kkd.kkdapp.adapter;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.fragment.WalletFragment;
import com.kkd.kkdapp.http.entity.CoinShopDataItem;
import com.kkd.kkdapp.http.entity.MyProperty;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.view.CountView;
import com.kkd.kkdapp.view.shapeimageview.PorterShapeImageView;

public class ExchangeListNewAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater inflater;
	private List<CoinShopDataItem> list;
	private Dialog mDialog;
	private CountView myHappyBi_num_happyBi;
	private Handler handler = new Handler();
	private MyProperty myProperty;
	private String mCurrency = "";

	public ExchangeListNewAdapter(Context context, List<CoinShopDataItem> list,
			CountView myHappyBi_num_happyBi) {
		mContext = context;
		inflater = LayoutInflater.from(mContext);
		this.list = list;
		this.myHappyBi_num_happyBi = myHappyBi_num_happyBi;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_list_myhappycoinnew,
					parent, false);
			holder.tv_num_item = (TextView) convertView
					.findViewById(R.id.tv_num_item);
			holder.tv_type_item = (TextView) convertView
					.findViewById(R.id.tv_type_item);
			holder.tv_need_happybi_num_item = (TextView) convertView
					.findViewById(R.id.tv_need_happybi_num_item);
			holder.tv_toexchange_item = (TextView) convertView
					.findViewById(R.id.tv_toexchange_item);
			holder.tv_limit_item = (TextView) convertView
					.findViewById(R.id.tv_limit_item);
			holder.tv_time_item = (TextView) convertView
					.findViewById(R.id.tv_time_item);
			holder.linear_top = (LinearLayout) convertView
					.findViewById(R.id.linear_top);
			holder.footerView = convertView.findViewById(R.id.footerView);
			holder.unit_img = (ImageView) convertView
					.findViewById(R.id.unit_img);
			holder.unit = (TextView) convertView.findViewById(R.id.unit);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position == 0) {
			holder.linear_top.setVisibility(View.VISIBLE);
		} else {
			holder.linear_top.setVisibility(View.GONE);
		}
		if (position == list.size() - 1) {
			holder.footerView.setVisibility(View.VISIBLE);
		} else {
			holder.footerView.setVisibility(View.GONE);
		}
		holder.tv_toexchange_item
				.setOnClickListener(new OnSingleClickListener() {

					@Override
					public void onSingleClick(View v) {
						requestNetData(list.get(position));
						showDialog();
					}
				});
		if (list.get(position).getAmount() % 100 == 0) {
			holder.tv_num_item.setText(list.get(position).getAmount() / 100
					+ "");
		} else {
			holder.tv_num_item.setText(list.get(position).getAmount() * 0.01
					+ "");
		}
		holder.tv_need_happybi_num_item.setText(list.get(position)
				.getCoinCount() + "");
		holder.tv_type_item.setText(list.get(position).getDesc());
		holder.tv_time_item.setText("兑换后" + list.get(position).getDayOver()
				+ "天内有效");
		if (list.get(position).getType() == 1) {
			holder.tv_limit_item.setText("起投金额"
					+ (list.get(position).getLowestAmount() / 100) + "元");
		} else if (list.get(position).getType() == 4) {
			holder.tv_limit_item.setText("冲抵提现手续费");
		}
		holder.unit_img.setVisibility(View.VISIBLE);
		holder.unit.setVisibility(View.GONE);
		if (list.get(position).getType() == 2) {
			holder.unit_img.setVisibility(View.GONE);
			holder.unit.setVisibility(View.VISIBLE);
			holder.unit.setText("+");
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv_num_item;
		TextView tv_type_item;
		TextView tv_need_happybi_num_item;
		TextView tv_toexchange_item;
		TextView tv_limit_item;
		TextView tv_time_item;
		LinearLayout linear_top;
		View footerView;
		ImageView unit_img;
		TextView unit;
	}

	private void requestNetData(final CoinShopDataItem coinShopDataItem) {
		KkdHttpMagager.getInstance().coinChange(
				coinShopDataItem.getCoinCount(),
				coinShopDataItem.getAmount() % 100 == 0 ? coinShopDataItem
						.getAmount() / 100 + "" : coinShopDataItem.getAmount()
						* 0.01 + "", coinShopDataItem.getType(),
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mCurrency = responseBody.toString();
						if (mDialog != null) {
							mDialog.dismiss();
						}
						WalletFragment.flag_isExchangeHappBi = true;
						AppUtil.makeToastForCustomer(mContext,
								"兑换成功,稍后将优惠券\n放入您的账户中。");
						myHappyBi_num_happyBi.showNumberWithAnimation(
								myHappyBi_num_happyBi.getNumber(),
								Integer.parseInt(mCurrency)
										- myHappyBi_num_happyBi.getNumber(),
								1000);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						if (mDialog != null) {
							mDialog.dismiss();
						}
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				});
	}

	private void showDialog() {
		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		try {
			mDialog = DialogUtils.toLoadDialog(mContext, "兑换中,请稍候...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void destroyDialog(){
		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
	}

}
